package org.budo.support.servlet.filter.origin.cross;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lmw
 */
public class BudoCrossOriginFilter extends AbstractBudoCrossOriginFilter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        if (HTTP_METHOD_OPTIONS.equals(httpRequest.getMethod())) {
            httpResponse.setHeader(ACCESS_CONTROL_ALLOW_HEADERS, this.getAccessControlAllowHeaders());
            httpResponse.setStatus(204);
        }

        httpResponse.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, this.getAccessControlAllowOrigin());

        chain.doFilter(request, httpResponse);
    }
}
